package com.sdry.web.controller.hpf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sdry.model.hpf.ApprovalRecord;
import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.PickApply;
import com.sdry.model.llm.Department;
import com.sdry.model.llm.User;
import com.sdry.service.hpf.PickApplyService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: PickApplyController 
 * @Description:领料申请单Controller
 * @Author hpf
 * @Date 2018年10月9日 下午3:12:57 
 * @version 1.0
 */
@RequestMapping(value="/pickApply")
@Controller
public class PickApplyController {

	@Resource
	private PickApplyService pickApplyService;
	
	/**
	 * 分页查询领料申请单
	 * @param page 分页条件
	 * @param pickApply 查询条件
	 * @param response
	 */
	@RequestMapping(value="/listPagePickApply")
	public void listPagePickApply(Page page,PickApply pickApply,HttpServletResponse response){
		Map<String, Object> map = new HashMap<>();
		map.put("page", page.getPage());
		map.put("limit", page.getLimit());
		if (pickApply.getPickNum() != null) {
			map.put("pickNum", pickApply.getPickNum());
		} else {
			map.put("pickNum", "");
			pickApply.setPickNum("");
		}
		List<PickApply> pickApplyList = pickApplyService.listPagePickApply(map);
		int count = pickApplyService.countPickApply(pickApply);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(pickApplyList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询详情
	 * @param pickID
	 * @param response
	 */
	@RequestMapping(value="/findPickApplyByID")
	public void findPickApplyByID(Long pickID,HttpServletResponse response){
		PickApply pickApply = pickApplyService.findPickApplyByID(pickID);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(pickApply, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", 10);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 保存领料申请单
	 * @param pickApply 领料申请单实体
	 * @param stockNum 存货编码组成的数组
	 * @param stockName 存货名称组成的数组
	 * @param stockModel 规格型号组成的数组
	 * @param paunit 主计量单位组成的数组
	 * @param packNorm 包装规格组成的数组
	 * @param batch 批次号组成的数组
	 * @param quantity 数量组成的数组
	 * @param response
	 */
	@RequestMapping(value="/savePickApply", method=RequestMethod.POST)
	public void savePickApply(PickApply pickApply,String[] stockNum,String[] stockName,String[] stockModel,
			String[] paunit,String[] packNorm,String[] batch,String[] quantity,HttpServletResponse response,HttpSession session){
		int isSuccess = 0;
		User user = (User) session.getAttribute("currentUser");
		Department department = (Department) session.getAttribute("department");
		pickApply.setPickDept(department.getDid());
		pickApply.setPickPerson(user.getUid());
		if (pickApply.getPickID() == null || "".equals(pickApply.getPickID())) {
			String[] pNorm = new String[1];
			if (packNorm.length == 0) {
				pNorm[0] = "";
				isSuccess = pickApplyService.insertPickApply(pickApply,stockNum,stockName,stockModel,paunit,pNorm,batch,quantity);
			} else {
				isSuccess = pickApplyService.insertPickApply(pickApply,stockNum,stockName,stockModel,paunit,packNorm,batch,quantity);
			}
		} else {
			isSuccess = pickApplyService.updatePickApply(pickApply,stockNum,stockName,stockModel,quantity);
		}
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 批量删除领料申请单
	 * @param pickIDs 领料申请单ID拼接字符串（以，分割）
	 * @param response
	 */
	@RequestMapping(value="/deletePickApply")
	public void deletePickApply(String pickIDs,HttpServletResponse response){
		int isSuccess = pickApplyService.deletePickApply(pickIDs);
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 批量提交领料申请单审核
	 * @param pickIDs 领料申请单ID拼接字符串（以，分割）
	 * @param response
	 */
	@RequestMapping(value="/approvalPickApply")
	public void approvalPickApply(String pickIDs,Long ztree,HttpServletResponse response,HttpSession session){
		int isSuccess = pickApplyService.approvalPickApply(pickIDs,ztree,session);
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 生成领料单
	 * @param remark1 排产单日期
	 * @param response
	 */
	@RequestMapping(value="/generatePickApply")
	public void generatePickApply(String remark1,HttpServletResponse response){
		PickApply pickApply = pickApplyService.generatePickApply(remark1);
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(pickApply, jsonConfig);
		try {
			if (pickApply == null) {
				ResponseUtil.write(response, 0);
			} else {
				ResponseUtil.write(response, jsonArray);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 撤销审批
	 * @param approvalRecord
	 * @param response
	 * @param session
	 */
	@RequestMapping(value="/cancelPickApply")
	public void cancelPickApply(ApprovalRecord approvalRecord,HttpServletResponse response,HttpSession session){
		int isSuccess = pickApplyService.cancelPickApply(approvalRecord,session);
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 审核领料申请单
	 * @param pickID 领料申请单ID
	 * @param response
	 */
	@RequestMapping(value="/approvalPickApply2")
	public void approvalPickApply2(ApprovalRecord approvalRecord,HttpServletResponse response,HttpSession session){
		int isSuccess = pickApplyService.approvalPickApply2(approvalRecord,session);
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 审核领料申请单
	 * @param pickID 领料申请单ID
	 * @param response
	 */
	@RequestMapping(value="/approvalAndroidPickApply")
	public void approvalAndroidPickApply(String pickID,ApprovalRecord approvalRecord,HttpServletResponse response,HttpSession session){
		JSONObject jsonObject = new JSONObject();
		List<String> msgList = new ArrayList<>();
		approvalRecord.setPickID(Long.parseLong(pickID));
		int isSuccess = pickApplyService.approvalPickApply2(approvalRecord,session);
		msgList.add("审批成功");
		JSONArray jsonArray = JSONArray.fromObject(msgList);
		try {
			if(isSuccess > 0){
				jsonObject.put("state", 1);
				jsonObject.put("message", "审批成功");
				jsonObject.put("data", jsonArray);
			}else{
				jsonObject.put("state", 0);
				jsonObject.put("message", "审批失败");
				jsonObject.put("data", null);
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
